PROJ	:=
EMPTY	:=
SPACE	:= $(EMPTY) $(EMPTY)
SLASH	:= /

V       := @

# ARCH	:= arch/arm/s3c2410/
ARCH	:= 

SKYEYE := skyeye

GCCPREFIX := arm-elf-

# eliminate default suffix rules
.SUFFIXES: .c .S .h

# delete target files if there is an error (or make is interrupted)
.DELETE_ON_ERROR:

# define compiler and flags

HOSTCC		:= gcc
HOSTCFLAGS	:= -g -Wall -O2

GDB		:= $(GCCPREFIX)gdb

CC		:= $(GCCPREFIX)gcc
CFLAGS	:= -fno-builtin -Wall -ggdb -gstabs -nostdinc -nostartfiles
CFLAGS	+= $(shell $(CC) -E -x c /dev/null >/dev/null 2>&1 && echo )
CTYPE	:= c S

LD      := $(GCCPREFIX)ld
LDFLAGS	:= 
LDFLAGS	+= -nostdlib

OBJCOPY := $(GCCPREFIX)objcopy
OBJDUMP := $(GCCPREFIX)objdump

COPY	:= cp
MKDIR   := mkdir -p
MV		:= mv
RM		:= rm -f
AWK		:= awk
SED		:= sed
SH		:= sh
TR		:= tr
TOUCH	:= touch -c

OBJDIR	:= obj
BINDIR	:= bin

ALLOBJS	:=
ALLDEPS	:=
TARGETS	:=

include tools/function.mk

listf_cc = $(call listf,$(1),$(CTYPE))

# for cc
add_files_cc = $(call add_files,$(1),$(CC),$(CFLAGS) $(3),$(2),$(4))
create_target_cc = $(call create_target,$(1),$(2),$(3),$(CC),$(CFLAGS))

# for hostcc
add_files_host = $(call add_files,$(1),$(HOSTCC),$(HOSTCFLAGS),$(2),$(3))
create_target_host = $(call create_target,$(1),$(2),$(3),$(HOSTCC),$(HOSTCFLAGS))

cgtype = $(patsubst %.$(2),%.$(3),$(1))
objfile = $(call toobj,$(1))
asmfile = $(call cgtype,$(call toobj,$(1)),o,asm)
outfile = $(call cgtype,$(call toobj,$(1)),o,out)
symfile = $(call cgtype,$(call toobj,$(1)),o,sym)

# for match pattern
match = $(shell echo $(2) | $(AWK) '{for(i=1;i<=NF;i++){if(match("$(1)","^"$$(i)"$$")){exit 1;}}}'; echo $$?)

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# include kernel/user

INCLUDE	+= libs/

CFLAGS	+= $(addprefix -I,$(INCLUDE))

LIBDIR	+= libs

$(call add_files_cc,$(call listf_cc,$(LIBDIR)),libs,)

# -------------------------------------------------------------------
# kernel

KINCLUDE	+= kern/debug/ \
				$(ARCH)kern/driver/ \
				$(ARCH)kern/trap/


KSRCDIR		+= kern/init \
				kern/libs \
				kern/debug \
				$(ARCH)kern/driver \
			   $(ARCH)kern/trap

KCFLAGS		+= $(addprefix -I,$(KINCLUDE))

$(call add_files_cc,$(call listf_cc,$(KSRCDIR)),kernel,$(KCFLAGS))

KOBJS	= $(call read_packet,kernel libs)

# create kernel target
kernel = $(call totarget,kernel)

$(kernel): tools/kernel.ld

$(kernel): $(KOBJS)
	@echo + ld $@
	$(V)$(LD) $(LDFLAGS) -T tools/kernel.ld -o $@ $(KOBJS)
	@$(OBJDUMP) -S $@ > $(call asmfile,kernel)
	@$(OBJDUMP) -t $@ | $(SED) '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(call symfile,kernel)

$(call create_target,kernel)

# -------------------------------------------------------------------

# create bootblock
bootfiles = $(call listf_cc,$(ARCH)boot)
$(foreach f,$(bootfiles),$(call cc_compile,$(f),$(CC),$(CFLAGS) -Os -nostdinc))

bootblock = $(call totarget,bootblock)

$(bootblock): $(call toobj,$(bootfiles))
	@echo + ld $@
	$(V)$(LD) $(LDFLAGS) -N -e _start -T$(ARCH)boot/bootlink.lds $^ -o $(call toobj,bootblock)
	$(V)$(OBJDUMP) -S $(call objfile,bootblock) > $(call asmfile,bootblock)
	$(V)$(OBJCOPY) -S -O elf32-littlearm $(call objfile,bootblock) $(bootblock)
	
$(call create_target,bootblock)

# -------------------------------------------------------------------


# create ucore.img
#UCOREIMG        := $(call totarget,ucore.img)

#$(UCOREIMG): $(kernel) $(bootblock)
#	$(V)dd if=/dev/zero of=$@ count=10000
#	$(V)dd if=$(bootblock) of=$@ conv=notrunc
#	$(V)dd if=$(kernel) of=$@ seek=1 conv=notrunc

#$(call create_target,ucore.img)

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


$(call finish_all)

ifneq ($(MAKECMDGOALS),clean)
-include $(ALLDEPS)
endif

TARGETS: $(TARGETS)

.DEFAULT_GOAL := TARGETS

.PHONY: $(SKYEYE)
$(SKYEYE):
	$(SKYEYE) -e $(bootblock)
	
.PHONY: retest
retest:
	make clean
	make
	make $(SKYEYE)

.PHONY: clean
clean:
	-$(RM) -r $(OBJDIR) $(BINDIR)
